/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.engin.archive;

import ac.settings.dataModel;
import ac.settings.files;
import static ac.settings.files.REPORT_clinet_status_print;
import static ac.settings.rowsStatus.rowDeleted;
import ac.settings.sqlQuerys;
import ac.ui.utils;
import ac.uitls.propertiesOperation;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ismail
 */
public class clients extends ac.ui.FrameOperationTemplat implements dataModel, files {

    double balance = 0;

    String Table_balance = TABLE_clients_balance;
    String Table_history = TABLE_clients_move_history;
    String Tabel = TABLE_CLIENTS_DATA;
    propertiesOperation po = new propertiesOperation();
    ac.lib.database.operations db = new ac.lib.database.operations();

    /**
     *
     * @param id
     * @return
     */
    public String[] getRow(String id) {
        String[] r;
        r = db.getRow(Tabel, "*", "id='" + id + "'");
        return r;

    }

    private int Bop(String id, double data, String Status) {
        Double newB = 0d;
        Double oldB = 0d;

        String[] oldb_data = db.getRow(Table_balance, "*", "client_id='" + id + "'");

        if (oldb_data[2] != null && !oldb_data[2].equalsIgnoreCase("null")) {
            oldB = Double.parseDouble(oldb_data[2]);
        }

        if (Status.equals("-")) {

            newB = oldB - data;
        } else {
            newB = oldB + data;
        }

        if (oldb_data[2] != null && !oldb_data[2].equalsIgnoreCase("null")) {
            String[][] usedata = {new String[]{"balance"}, new String[]{"" + newB}};
            db.setUpdate(Table_balance, usedata, "client_id='" + id + "'");
        } else {

            String[][] usedata = {new String[]{"client_id", "balance"}, new String[]{id, "" + newB}};
            db.setInsert(Table_balance, usedata);

        }
        ac.GUI.index.updateCinfo(id, true);
        return db.retuenstatus;
    }

    /**
     *
     * @param id
     * @param data
     * @return
     */
    public int addTobalance(String id, double data) {

        int returnstatus = Bop(id, data, "+");

        return returnstatus;

    }

    /**
     *
     * @param id
     * @param data
     * @return
     */
    public int Cutbalance(String id, double data) {
        int returnstatus = Bop(id, data, "-");

        return returnstatus;

    }

    /**
     *
     * @param id
     */
    public void removebalance(String id) {
        db.setDelete(Table_balance, "client_id='" + id + "'");

    }

    /**
     *
     */
    public void removeAll() {

        if (utils.sendRemoveMssage(null) == 0) {
            try {

                db.setOpenCon();

                ResultSet r = db.getDataWithOutCon(TabelString, "*", "rowstatus ='" + rowDeleted + "'");
                while (r.next()) {

                    String myid = r.getString("id");

                    db.setDeleteWithOutCon(TabelString, Integer.parseInt(myid));
                    db.setDeleteWithOutCon(Table_balance, "client_id='" + myid + "'");
                }

                db.setCloseCon();
            } catch (SQLException ex) {
                Logger.getLogger(clients.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     *
     * @param id
     * @return
     */
    public double getbalanceLimit(String id) {
        double d = 0d;
        String[] oldb_data = getRow(id);
        if (oldb_data[11] != null) {

            d = Double.parseDouble(oldb_data[11]);
        }

        return d;
    }

    /**
     *
     * @param id
     * @return
     */
    public double getbalance(String id) {

        double d = 0d;

        String[] oldb_data = db.getRow(Table_balance, "*", "client_id='" + id + "'");

        if (oldb_data[2] != null) {

            d = Double.parseDouble(oldb_data[2]);
        }

        return d;

    }

    /**
     *
     * @param id
     * @param data
     * @return
     */
    public int getFrombalance(String id, double data) {
        int r = 0;

        return r;

    }

    /**
     *
     * @param id
     * @return
     */
    public String[] getInfo(String id) {
        String[] r = null;

        return r;

    }

    private void getoldbalance() {
    }

    /**
     *
     * @param move
     */
    public void addTohistory(String[] move) {
    }
    //<editor-fold defaultstate="collapsed" desc="reportes & print">

    /**
     *
     * @param catid
     * @param type
     */
    public void reportesClinteStatus(String catid, String type) {

        String where = "where 1=1"
                + "";
        if (catid != null && !catid.equals("")) {

            where += " and  clients_data.cat_id = '" + catid + "'  ";
        }

        if (type != null) {

            if (type.equals("c")) {
                where += " and  (clients_data.type='c' or  clients_data.type='a' )";
            } else {

                where += " and  (clients_data.type=='s' or  clients_data.type='a' )";

            }

        }

        ac.reportes.reportes.updateReporteTitles(po.getWord("title.retportClinteStatus"),
                "-",
                "-",
                "-",
                "-");

        setPrint(REPORT_clinet_status_print, sqlQuerys.SQL_ClineteAll + where);

    }

    /**
     *
     * @param id
     */
    public void reportesClinteFile(String id) {

        setPrint(REPORT_clinet_file_print, sqlQuerys.SQL_ClineteFile + id);

    }

//</editor-fold>
}
